﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dapper.Moon
{
    internal class InsertableBulkBatchImpl : InsertableBatchImpl
    {
        public override int Execute()
        {
            if (batchs == null || batchs.Count == 0) return 0;
            int count = 0;
            foreach (var item in batchs)
            {
                count += Convert.ToInt32(item.GetType().GetMethod("BulkInsert").Invoke(item, null));
            }
            return count;
        }

        public override async Task<int> ExecuteAsync()
        {
            if (batchs == null || batchs.Count == 0) return 0;
            int count = 0;
            foreach (var item in batchs)
            {
                var methodObj = item.GetType().GetMethod("BulkInsertAsync");
                Task task = methodObj.Invoke(item, null) as Task;
                await task;
                count += Convert.ToInt32(task.GetType().GetProperty("Result").GetValue(task, null));
            }
            return count;
        }
    }
}
